package com.xinfang.superminer.view.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.xinfang.superminer.R;
import com.xinfang.superminer.adapter.SelectListviewAdapter;

import java.util.List;

/**
 * 筛选pop
 * Created by Administrator on 2018/3/28.
 */

public class StateSelectPop extends PopupWindow implements View.OnClickListener {

    private Context context;
    private View view;



    private OnSelectListener onSelectListener;
    private SelectListviewAdapter selectListviewAdapter;

    private List<String> list;
    public StateSelectPop(Context context, OnSelectListener onSelectListener,List<String> list) {
        super(context);
        this.context = context;
        this.onSelectListener=onSelectListener;
        this.list=list;
        initPop();

        initView();

    }

    private void initView() {
        View v=view.findViewById(R.id.view_null);
        v.setOnClickListener(this);
        ListView listView=view.findViewById(R.id.lv_houselayout);
        selectListviewAdapter = new SelectListviewAdapter(context,list);
        listView.setAdapter(selectListviewAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                onSelectListener.selectState(i,list.get(i));
                selectListviewAdapter.setSelected(i);
                dismiss();
            }
        });
    }


    /**
     * 初始化pop样式
     */
    private void initPop() {

        view= LayoutInflater.from(context).inflate(R.layout.pop_houseayoutselect,null);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);


        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        this.setContentView(view);


        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.gray_bg));
        //设置SelectPicPopupWindow弹出窗体的背景
        setBackgroundDrawable(dw);

        //获取popwindow焦点
        setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        setOutsideTouchable(true);
    }

    /**\
     * 设置透明背景
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
//        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
//        lp.alpha = bgAlpha; //0.0-1.0
//        context.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {

            case R.id.view_null:
                //点击半透明
                dismiss();
                break;

        }
    }

    /**
     * 房型选择回调
     */
    public  interface OnSelectListener
    {
        public void selectState(int i, String content);//不限

    }
}
